//
//  RoomMateApplyViewController.m
//  Fangmm
//
//  Created by ChaoZhou on 16/3/21.
//  Copyright © 2016年 com.fangmm. All rights reserved.
//

#import "RoomMateApplyViewController.h"
#import "HttpServer.h"
#import "Apply.h"
#import "RoomMateApplyTableViewCell.h"
#import <UIButton+WebCache.h>
#import <UIImageView+WebCache.h>
#import "UserHomepageViewController.h"
#import <TSMessages/TSMessageView.h>
@interface RoomMateApplyViewController ()<UITableViewDataSource,UITableViewDelegate,RoomMateApplyTableViewCellDelegate>
{
    NSMutableArray *applyArray;
}

@end

@implementation RoomMateApplyViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"申请列表";
    UIBarButtonItem *leftBar = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"back-18-36"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(goToBack)];
    self.navigationItem.leftBarButtonItem = leftBar;
    applyArray = [NSMutableArray array];
    _mainTableView.rowHeight = 50;
    [_mainTableView registerNib:[UINib nibWithNibName:@"RoomMateApplyTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"RoomMateApplyTableViewCell"];
    _mainTableView.tableFooterView = [UIView new];
    [self getApplicationList];
    // Do any additional setup after loading the view from its nib.
}
-(void)goToBack
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return applyArray.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RoomMateApplyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RoomMateApplyTableViewCell"];
    cell.delegate = self;
    cell.acceptButtonView.tag = indexPath.row;
    cell.delineButtonView.tag = indexPath.row;
    cell.headerButton.tag = indexPath.row;
    Apply *apply = applyArray[indexPath.row];
    [cell.headerButton sd_setBackgroundImageWithURL:[NSURL URLWithString:apply.userAvatar] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"houseown"]];
    [cell.nameLabel setText:apply.userName];
    [cell.seatLabel setText:apply.roomName];
    if ([apply.userGender isEqualToString:@"f"])
    {
        [cell.sexLabel setImage:[UIImage imageNamed:@"girl_22_22"]];
    }
    else
    {
        [cell.sexLabel setImage:[UIImage imageNamed:@"boy_22_22"]];
    }
    cell.userId = ((Apply*)(applyArray[indexPath.row])).userId;
    cell.userName = ((Apply*)(applyArray[indexPath.row])).userName;
    return cell;
}
-(void)acceptWithButton:(UIButton *)sender
{
    NSLog(@"通过申请");
    Apply *apply = applyArray[sender.tag];
    [HttpServer passApplyWithTeamId:apply.teamId WithApply:apply.applyId withSuccessBlock:^(NSDictionary *responseData)
    {
        NSLog(@"通过申请%@",responseData);
        [self getApplicationList];
        [_mainTableView reloadData];
        [TSMessage showNotificationInViewController:self title:@"申请已通过!" subtitle:responseData[@"msg"] type:TSMessageNotificationTypeSuccess duration:2.0 canBeDismissedByUser:true];
    }
    withFailedBlock:^(id errorMsg)
    {
        NSLog(@"通过cuowu%@",errorMsg);
        [TSMessage showNotificationInViewController:self title:@"通过申请失败!" subtitle:errorMsg[@"msg"] type:TSMessageNotificationTypeError duration:2.0 canBeDismissedByUser:true];
    }];
}
-(void)delineWithButton:(UIButton *)sender
{
    NSLog(@"拒绝申请");
    Apply *apply = applyArray[sender.tag];
    [HttpServer rejectApplyWithTeamId:apply.teamId WithApply:apply.applyId withSuccessBlock:^(NSDictionary *responseData)
    {
        NSLog(@"拒绝申请%@",responseData);
        [self getApplicationList];
        [TSMessage showNotificationInViewController:self title:@"申请已拒绝!" subtitle:@"" type:TSMessageNotificationTypeSuccess duration:2.0 canBeDismissedByUser:true];
        //[_mainTableView reloadData];
        
    } withFailedBlock:^(id errorMsg)
    {
        NSLog(@"拒绝申请cuowu%@",errorMsg);
    }];
}
-(void)getApplicationList
{
    [HttpServer getApplicationListWithTeamId:self.teamId withPage:1 withPageSize:10 withSuccessBlock:^(NSDictionary *responseData)
     {
         NSLog(@"申请列表＝＝＝＝%@",responseData);
         NSArray *resourceData = responseData[@"applys"];
         [applyArray removeAllObjects];
         for (NSDictionary *dic in resourceData)
         {
             Apply *apply = [[Apply alloc]initWithDic:dic];
             [applyArray addObject:apply];
         }
         [_mainTableView reloadData];
     }
     withFailedBlock:^(id errorMsg)
     {
         NSLog(@"申请列表cuowu＝＝＝＝%@",errorMsg);
     }];
}
-(void)clickHeaderViewWithUserId:(NSString *)userId withUserName:(NSString *)userName
{
    UserHomepageViewController *userHomePageVC = [[UserHomepageViewController alloc]init];
    userHomePageVC.userId = userId;
    userHomePageVC.userName = userName;
    [self.navigationController pushViewController:userHomePageVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
